<?php
/**
 * @copyright	Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
 * @license		GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;
jimport('joomla.application.component.modeladmin');
class ExercisesModelQuestion extends JModelAdmin
{
	protected function canDelete($record)
	{
		if (!empty($record->id)) {
				if ($record->state != -2) {
					return ;
				}
			$user = JFactory::getUser();

			if (!empty($record->cate_id)) {
				return $user->authorise('core.delete', 'com_exercises.category.'.(int) $record->cate_id);
			}
			else {
				return $user->authorise('core.delete', 'com_exercises');
			}
		}
	}
	protected function canEditState($record)
	{
		$user = JFactory::getUser();

		if (!empty($record->cate_id)) {
			return $user->authorise('core.edit.state', 'com_exercises.category.'.(int) $record->cate_id);
		}
		else {
			return $user->authorise('core.edit.state', 'com_exercises');
		}
	}
	public function getTable($type = 'Question', $prefix = 'ExercisesTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}
	public function getForm($data = array(), $loadData = true)
	{
		// Get the form.
		$form = $this->loadForm('com_exercises.question', 'question', array('control' => 'jform', 'load_data' => $loadData));
		if (empty($form)) {
			return false;
		}

		return $form;
	}
	protected function loadFormData()
	{
		// Check the session for previously entered form data.
		$data = JFactory::getApplication()->getUserState('com_exercises.edit.question.data', array());

		if (empty($data)) {
			$data = $this->getItem();
		}

		return $data;
	}
	protected function prepareTable(&$table)
	{
		$table->name = htmlspecialchars_decode($table->name, ENT_QUOTES);
	}
}
